﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SES.CMS.BL;
using SES.CMS.DO;
namespace SES.CMS.AdminCP
{
    public partial class Config : System.Web.UI.Page
    {
        sysConfigDO objConfig = new sysConfigDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            
                if (Request.QueryString["ConfigID"] != null)
                {
                    objConfig.ConfigID = int.Parse(Request.QueryString["ConfigID"].ToString());
                 if(!IsPostBack)
                    initForm();
                }
            
           
        }

        private void initForm()
        {
            objConfig = new sysConfigBL().Select(objConfig);
            txtTitle.Text = objConfig.ConfigName;
            
            if (objConfig.ConfigID == 9 || objConfig.ConfigID == 26 || objConfig.ConfigID == 5 || objConfig.ConfigID == 13 || objConfig.ConfigID == 29 || objConfig.ConfigID == 32)
            {
                txtCKX.Content = objConfig.ConfigValue;
                trDes.Visible = false;
                trSupport.Visible = true;
            }
            else
                txtDescription.Text = objConfig.ConfigValue;
            chkActive.Checked = objConfig.IsActive;
            if (objConfig.ConfigID == 31 || objConfig.ConfigID == 4)
            {
                trPopup.Visible = true;
                trDes.Visible = false;
                txtLink.Text = objConfig.ConfigValue;
                if (objConfig.ConfigID == 4)
                    hplImage.NavigateUrl = "/Media/quang-cao-trai.jpg";
                if (objConfig.ConfigID == 31)
                    hplImage.NavigateUrl = "/Media/quang-cao-phai.jpg";
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            initObject();
            if (objConfig.ConfigID <= 0)
            {
                new sysConfigBL().Insert(objConfig);
            }
            else
            {
               int ConfigId = int.Parse(Request.QueryString["ConfigID"].ToString());
               objConfig.ConfigID = ConfigId;
                new sysConfigBL().Update(objConfig);
            }
            Functions.Alert("Cập nhật thành công!", "ListConfig.aspx");
        }

        private void initObject()
        {
            objConfig = new sysConfigBL().Select(objConfig);
            int Order1 = int.Parse(objConfig.OrderID.ToString());
            objConfig.ConfigName = txtTitle.Text;
            if (objConfig.ConfigID == 5  || objConfig.ConfigID == 9 || objConfig.ConfigID == 13 || objConfig.ConfigID == 26 || objConfig.ConfigID == 29 || objConfig.ConfigID == 32)
                objConfig.ConfigValue = txtCKX.Content;
            else if (objConfig.ConfigID == 31 || objConfig.ConfigID == 4)
            {
                UploadFile(fuImage);
                objConfig.ConfigValue = txtLink.Text.Trim();
            }
            else
            objConfig.ConfigValue = txtDescription.Text;
            objConfig.IsActive = chkActive.Checked;
            objConfig.OrderID = Order1;
        }
        private string UploadFile(FileUpload fulImages)
        {
            if (!string.IsNullOrEmpty(fulImages.FileName))
            {
                string FileName = "";
                if (objConfig.ConfigID == 4)
                    FileName = "quang-cao-trai.jpg";
                if (objConfig.ConfigID == 31)
                    FileName = "quang-cao-phai.jpg";

                string SaveLocation = string.Format("{0}\\{1}", Server.MapPath("~/Media/"), FileName);
                if (System.IO.File.Exists(SaveLocation))
                    System.IO.File.Delete(SaveLocation);
                fulImages.SaveAs(SaveLocation);
                return FileName;
            }
            return string.Empty;
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect("ListConfig.aspx");
        }
    }
    
}